package com.web.base.wechat.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blue.jdbc.JdbcDataSource;
import com.blue.jdbc.Page;
import com.web.base.wechat.exception.WechatException;
import com.web.base.wechat.dao.WechatDao;
import com.web.base.wechat.model.Wechat;
import com.web.core.util.Const;

@Service
@Transactional
@JdbcDataSource(Const.DS_BASE)
public class WechatService
{
	@Autowired
	private WechatDao wechatDao;
	
	public WechatService()
	{
	}
	
	public Wechat save(Wechat wechat)
	{
		if (wechatDao.exist(wechat, "code"))
			throw new WechatException("编号【" + wechat.getCode() + "】已经存在");
		
		if (wechat.getId() == null)
		{
			wechatDao.save(wechat);
		}
		else
		{
			wechatDao.update(wechat);
		}
		Wechat w = wechatDao.get(wechat.getId());
		return w;
	}
	
	public Wechat get(Integer id)
	{
		Wechat wechat = wechatDao.get(id);
		return wechat;
	}
	
	public Wechat getByCode(String code)
	{
		Wechat wechat = wechatDao.getByCode(code);
		return wechat;
	}
	
	public List<Wechat> list(Wechat wechat)
	{
		List<Wechat> wechatList = wechatDao.list(wechat);
		return wechatList;
	}
	
	public Page<Wechat> list(Wechat wechat, Page<Wechat> page)
	{
		page = wechatDao.list(wechat, page);
		return page;
	}
	
}
